package cn.qyj.oa.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qyj.oa.base.DaoSupportImpl;
import cn.qyj.oa.domain.Forum;
import cn.qyj.oa.service.ForumService;

@Service
@Transactional
@SuppressWarnings("unchecked")
public class ForumServiceImpl extends DaoSupportImpl<Forum> implements ForumService {

	public List<Forum> findAll() {

		return getSession().createQuery(//
				"FROM Forum f ORDER BY f.position")//
				.list();
	}

	public void save(Forum entity) {

		super.save(entity);
		//���õ�����ֵ�ٻ�������
		entity.setPosition(entity.getId().intValue());

	}

	public void moveDown(Long id) {
		Forum forum = getById(id);// ��ǰҪ�ƶ���
		// �ƶ������Ķ�����һ��
		Forum other = (Forum) getSession().createQuery(//
				"FROM Forum f WHERE f.position>? ORDER BY f.position")//
				.setParameter(0, forum.getPosition()).setFirstResult(0).setMaxResults(1).uniqueResult();
		// ����Position
		int temp = forum.getPosition();
		forum.setPosition(other.getPosition());
		other.setPosition(temp);

	}

	public void moveUp(Long id) {
		Forum forum = getById(id);// ��ǰҪ�ƶ���
		// �ƶ������Ķ�����һ��
		Forum other = (Forum) getSession().createQuery(//
				"FROM Forum f WHERE f.position<? ORDER BY f.position DESC")//
				.setParameter(0, forum.getPosition()).setFirstResult(0).setMaxResults(1).uniqueResult();
		// ����Position
		int temp = forum.getPosition();
		forum.setPosition(other.getPosition());
		other.setPosition(temp);

	}

}
